#include "GY_PatternComboBox.h"
#include <QDebug>

GY_PatternComboBox::GY_PatternComboBox(QWidget *parent)
    : QComboBox(parent)
{

}

void GY_PatternComboBox::addPatternDataItem(const QString &PatternName, const QString &PatternBitmap)
{
    if(PatternName.isNull() || PatternBitmap.isNull())
    {
        return;
    }

    QWidget* pwidget = addPatternWidget(PatternBitmap);
    pwidget->setStyleSheet("background-color:transparent;"); //设置透明背景
    pwidget->setFixedSize(20,20);
    QPixmap pixmap = pwidget->grab(pwidget->rect()); //获取图形截图
    this->addItem(QIcon(pixmap),PatternName);
    this->setSizeAdjustPolicy(QComboBox::AdjustToContents);
}

QWidget *GY_PatternComboBox::addPatternWidget(const QString &patternBitmap)
{
    QWidget* pWidget = new QWidget();
    QVBoxLayout* pVLayout = new QVBoxLayout();
    pVLayout->setMargin(0); //设置边距
    pVLayout->setSpacing(0); //设置间距
    for(int i=0;i<16;i++)
    {
        bool ok;
        int num = patternBitmap.split(" ").at(i).toInt(&ok,16);
        QString strStyle = QString::number(num,2); //将样式数值转换为二进制

        QHBoxLayout* pLayout = new QHBoxLayout();
        pLayout->setMargin(0); //设置边距
        pLayout->setSpacing(0); //设置间距
        if(strStyle.size()<16)
        {
            for(int k=0;k<16-strStyle.size();k++) {
                QLabel* pLabel = new QLabel(pWidget);
                pLabel->setStyleSheet("QLabel{background-color:transparent;}");
                pLayout->addWidget(pLabel);
            }
        }

        for(int j=0;j<strStyle.size();j++)
        {
            QLabel* pLabel = new QLabel(pWidget);

            if(strStyle[j]=="1")
            {
                pLabel->setStyleSheet("QLabel{background-color:black;}");
            }
            else {
                pLabel->setStyleSheet("QLabel{background-color:transparent;}");
            }

            pLayout->addWidget(pLabel);
        }
        pVLayout->addLayout(pLayout);
    }
    pWidget->setLayout(pVLayout);
    return pWidget;
}
